{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<a href=\"https://githubtocolab.com/gee-community/geemap/blob/master/docs/notebooks/146_chart_image_collection.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open in Colab\"/></a>\n",
    "\n",
    "**ImageCollection Charts**\n",
    "\n",
    "Uncomment the following line to install [geemap](https://geemap.org) if needed."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# %pip install -U geemap"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Import libraries"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import ee\n",
    "import geemap\n",
    "from geemap import chart"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "geemap.ee_initialize()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## image_series"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Define the forest feature collection.\n",
    "forest = ee.FeatureCollection(\"projects/google/charts_feature_example\").filter(\n",
    "    ee.Filter.eq(\"label\", \"Forest\")\n",
    ")\n",
    "\n",
    "# Load MODIS vegetation indices data and subset a decade of images.\n",
    "veg_indices = (\n",
    "    ee.ImageCollection(\"MODIS/061/MOD13A1\")\n",
    "    .filter(ee.Filter.date(\"2010-01-01\", \"2020-01-01\"))\n",
    "    .select([\"NDVI\", \"EVI\"])\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "title = \"Average Vegetation Index Value by Date for Forest\"\n",
    "x_label = \"Year\"\n",
    "y_label = \"Vegetation index (x1e4)\"\n",
    "colors = [\"#e37d05\", \"#1d6b99\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fig = chart.image_series(\n",
    "    veg_indices,\n",
    "    region=forest,\n",
    "    reducer=ee.Reducer.mean(),\n",
    "    scale=500,\n",
    "    x_property=\"system:time_start\",\n",
    "    chart_type=\"LineChart\",\n",
    "    x_cols=\"date\",\n",
    "    y_cols=[\"NDVI\", \"EVI\"],\n",
    "    colors=colors,\n",
    "    title=title,\n",
    "    x_label=x_label,\n",
    "    y_label=y_label,\n",
    "    legend_location=\"right\",\n",
    ")\n",
    "fig"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![](https://i.imgur.com/r9zSJh6.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## image_series_by_region"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Import the example feature collection.\n",
    "ecoregions = ee.FeatureCollection(\"projects/google/charts_feature_example\")\n",
    "\n",
    "# Load MODIS vegetation indices data and subset a decade of images.\n",
    "veg_indices = (\n",
    "    ee.ImageCollection(\"MODIS/061/MOD13A1\")\n",
    "    .filter(ee.Filter.date(\"2010-01-01\", \"2020-01-01\"))\n",
    "    .select([\"NDVI\"])\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "title = \"Average NDVI Value by Date\"\n",
    "x_label = \"Date\"\n",
    "y_label = \"NDVI (x1e4)\"\n",
    "x_cols = \"index\"\n",
    "y_cols = [\"Desert\", \"Forest\", \"Grassland\"]\n",
    "colors = [\"#f0af07\", \"#0f8755\", \"#76b349\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fig = chart.image_series_by_region(\n",
    "    veg_indices,\n",
    "    regions=ecoregions,\n",
    "    reducer=ee.Reducer.mean(),\n",
    "    band=\"NDVI\",\n",
    "    scale=500,\n",
    "    x_property=\"system:time_start\",\n",
    "    series_property=\"label\",\n",
    "    chart_type=\"LineChart\",\n",
    "    x_cols=x_cols,\n",
    "    y_cols=y_cols,\n",
    "    title=title,\n",
    "    x_label=x_label,\n",
    "    y_label=y_label,\n",
    "    colors=colors,\n",
    "    stroke_width=3,\n",
    "    legend_location=\"bottom-left\",\n",
    ")\n",
    "fig"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![](https://i.imgur.com/rnILSfI.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## image_doy_series"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Import the example feature collection and subset the grassland feature.\n",
    "grassland = ee.FeatureCollection(\"projects/google/charts_feature_example\").filter(\n",
    "    ee.Filter.eq(\"label\", \"Grassland\")\n",
    ")\n",
    "\n",
    "# Load MODIS vegetation indices data and subset a decade of images.\n",
    "veg_indices = (\n",
    "    ee.ImageCollection(\"MODIS/061/MOD13A1\")\n",
    "    .filter(ee.Filter.date(\"2010-01-01\", \"2020-01-01\"))\n",
    "    .select([\"NDVI\", \"EVI\"])\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "title = \"Average Vegetation Index Value by Day of Year for Grassland\"\n",
    "x_label = \"Day of Year\"\n",
    "y_label = \"Vegetation Index (x1e4)\"\n",
    "colors = [\"#f0af07\", \"#0f8755\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fig = chart.image_doy_series(\n",
    "    image_collection=veg_indices,\n",
    "    region=grassland,\n",
    "    scale=500,\n",
    "    chart_type=\"LineChart\",\n",
    "    title=title,\n",
    "    x_label=x_label,\n",
    "    y_label=y_label,\n",
    "    colors=colors,\n",
    "    stroke_width=5,\n",
    ")\n",
    "fig"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![](https://i.imgur.com/F0z088e.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## image_doy_series_by_year"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Import the example feature collection and subset the grassland feature.\n",
    "grassland = ee.FeatureCollection(\"projects/google/charts_feature_example\").filter(\n",
    "    ee.Filter.eq(\"label\", \"Grassland\")\n",
    ")\n",
    "\n",
    "# Load MODIS vegetation indices data and subset years 2012 and 2019.\n",
    "veg_indices = (\n",
    "    ee.ImageCollection(\"MODIS/061/MOD13A1\")\n",
    "    .filter(\n",
    "        ee.Filter.Or(\n",
    "            ee.Filter.date(\"2012-01-01\", \"2013-01-01\"),\n",
    "            ee.Filter.date(\"2019-01-01\", \"2020-01-01\"),\n",
    "        )\n",
    "    )\n",
    "    .select([\"NDVI\", \"EVI\"])\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "title = \"Average Vegetation Index Value by Day of Year for Grassland\"\n",
    "x_label = \"Day of Year\"\n",
    "y_label = \"Vegetation Index (x1e4)\"\n",
    "colors = [\"#e37d05\", \"#1d6b99\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fig = chart.doy_series_by_year(\n",
    "    veg_indices,\n",
    "    band_name=\"NDVI\",\n",
    "    region=grassland,\n",
    "    scale=500,\n",
    "    chart_type=\"LineChart\",\n",
    "    colors=colors,\n",
    "    title=title,\n",
    "    x_label=x_label,\n",
    "    y_label=y_label,\n",
    "    stroke_width=5,\n",
    ")\n",
    "fig"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![](https://i.imgur.com/ui6zpbl.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## image_doy_series_by_region"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Import the example feature collection and subset the grassland feature.\n",
    "ecoregions = ee.FeatureCollection(\"projects/google/charts_feature_example\")\n",
    "\n",
    "# Load MODIS vegetation indices data and subset a decade of images.\n",
    "veg_indices = (\n",
    "    ee.ImageCollection(\"MODIS/061/MOD13A1\")\n",
    "    .filter(ee.Filter.date(\"2010-01-01\", \"2020-01-01\"))\n",
    "    .select([\"NDVI\"])\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "title = \"Average Vegetation Index Value by Day of Year for Grassland\"\n",
    "x_label = \"Day of Year\"\n",
    "y_label = \"Vegetation Index (x1e4)\"\n",
    "colors = [\"#f0af07\", \"#0f8755\", \"#76b349\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fig = chart.image_doy_series_by_region(\n",
    "    veg_indices,\n",
    "    \"NDVI\",\n",
    "    ecoregions,\n",
    "    region_reducer=\"mean\",\n",
    "    scale=500,\n",
    "    year_reducer=ee.Reducer.mean(),\n",
    "    start_day=1,\n",
    "    end_day=366,\n",
    "    series_property=\"label\",\n",
    "    stroke_width=5,\n",
    "    chart_type=\"LineChart\",\n",
    "    title=title,\n",
    "    x_label=x_label,\n",
    "    y_label=y_label,\n",
    "    colors=colors,\n",
    "    legend_location=\"right\",\n",
    ")\n",
    "fig"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![](https://i.imgur.com/eGqGoRs.png)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "geo",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
